package inner

import (
	"encoding/json"
	util2 "file-server/test/util"
	"time"
)

type ApiParam struct {
	AccessKey   string                 `json:"accessKey"`
	AccessToken string                 `json:"-"`
	LotCode     string                 `json:"lotCode"`
	Ts          int64                  `json:"ts"`
	ReqId       string                 `json:"reqId"`
	Cmd         string                 `json:"cmd"`
	Sign        string                 `json:"sign"`
	Data        map[string]interface{} `json:"data"`
}

/**
签名并返回参数json字符串
*/
func (param *ApiParam) Get() string {

	param.Sign = util2.AuthSignCommon(&param.Data, param.AccessToken)

	b, _ := json.Marshal(param)

	return string(b)
}

func (param *ApiParam) Auth() map[string]interface{} {
	param.Ts = time.Now().Unix() * 1000
	//param.Ts = 1612404441047
	param.ReqId = util2.GetRandomString(32)
	//param.ReqId = "2f28bd1a5cc446f38e7481ffc9638648"

	header := make(map[string]interface{})
	header["API-TOKEN"] = util2.AuthHeader(param.AccessToken, param.Ts)
	header["kt-lotcodes"] = param.LotCode
	return header
}

func (param *ApiParam) Set(key string, val interface{}) {
	if param.Data == nil {
		param.Data = make(map[string]interface{})
	}
	param.Data[key] = val
}
